package com.smedi.ismedi.inmail.core.application.service.impl;

import com.smedi.ismedi.authority.starter.util.AuthorityUtil;
import com.smedi.ismedi.inmail.core.application.service.WeatherApplicationService;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.geo.Location;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.weather.WeatherDaily;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.weather.WeatherHourly;
import com.smedi.ismedi.inmail.core.domain.repository.LocationRepository;
import com.smedi.ismedi.inmail.core.domain.repository.WeatherDailyRepository;
import com.smedi.ismedi.inmail.core.domain.repository.WeatherHourlyRepository;
import com.smedi.ismedi.inmail.core.port.adapter.service.GeoService;
import com.smedi.ismedi.inmail.core.port.adapter.service.WeatherService;
import com.smedi.ismedi.inmail.core.port.adapter.service.dto.WeatherHistoryResponse;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * 通知公告应用服务实现类
 *
 * @author jiangqinxuan
 * @date 2021/07/26
 */
@Service
@RequiredArgsConstructor
public class WeatherApplicationServiceImpl implements WeatherApplicationService {
    private final WeatherService weatherService;
    private final GeoService geoService;
    private final LocationRepository locationRepository;
    private final WeatherHourlyRepository weatherHourlyRepository;
    private final WeatherDailyRepository weatherDailyRepository;

    private final DozerUtil dozerUtil;
    private final AuthorityUtil authorityUtil;

    @Value("${q.key}")
    private String qKey;

    @Override
    public String getWeather(String cityName, LocalDate date) {
        WeatherHourly result = new WeatherHourly();
        try {
            cityName = StringUtils.replace(cityName, "市", "");
            LocalTime localTime = LocalTime.of(10, 0, 0);
            LocalDateTime time = LocalDateTime.of(date, localTime);

            // 先查询数据库里面有没有
            String weather = weatherHourlyRepository.getWeatherByLocationIdAndTime(cityName, time);
            if (StringUtils.isNotBlank(weather)) {
                return weather;
            }

            long locationId;
            Location location = null;

            locationId = locationRepository.getLocationIdByName(cityName);

            if (locationId == 0L) {
                location = geoService.getLocation(cityName);
                if (null == location) {
                    return "多云";
                }
                locationRepository.insertLocation(location);
                locationId = location.getId();
            }

            if (locationId == 0L) {
                return "多云";
            }

            // 如果是当天的天气，请求实时天气
            if (date.isEqual(LocalDate.now())) {
                result = weatherService.getWeatherNow(locationId, qKey).getNow();
            } else {
                WeatherHistoryResponse weatherHistoryResponse = weatherService.getWeatherHistory(locationId, date, qKey);

                WeatherDaily weatherDaily = weatherHistoryResponse.getWeatherDaily();
                weatherDaily.setLocationId(locationId);
                weatherDaily.setName(cityName);
                weatherDailyRepository.insertWeatherDaily(weatherDaily);

                List<WeatherHourly> weatherHourly = weatherHistoryResponse.getWeatherHourly();
                result = weatherHourly.stream().filter(weatherHour -> weatherHour.getTime().isEqual(time)).findFirst().orElse(null);
            }

            if (null != result) {
                result.setLocationId(locationId);
                result.setName(cityName);
                weatherHourlyRepository.insertWeatherHourly(result);
            }

        } catch (Exception exception) {
            return "多云";
        }

        return result.getText();
    }
}